package com.jsle.financial.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.jsle.financial.domain.Resources;
import com.jsle.financial.domain.User;
import com.jsle.financial.domain.UserGroup;
import com.jsle.financial.service.ResourcesService;
import com.jsle.financial.util.AJAXUtil;
import com.jsle.financial.util.Constants;
import com.jsle.financial.util.Tools;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.Result;

/**
 * 资源管理Controller
 * @author wisdying
 * @date 2012-10-26
 */
@Controller
@RequestMapping("/resources")
public class ResourcesController {
	private Log log = LogFactory.getLog(ResourcesController.class);
	@Resource
	private ResourcesService resourcesService;
	
	/**
	 * 新增资源
	 */
	@RequestMapping(value = "/addResource.do",method=RequestMethod.POST)
	@ResponseBody
	public void addResource(Resources resources,HttpServletRequest request,HttpServletResponse response){
		Result rs = resourcesService.addResource(resources);
		if(rs.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("true"));
		}else{
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("false"));
		}
	}
	/**
	 * 删除单个资源
	 */
	@RequestMapping(value = "/delResource.do",method=RequestMethod.POST)
	@ResponseBody
	public void delResource(Resources resources,HttpServletRequest request,HttpServletResponse response){
		Result rs = resourcesService.delResources(resources);
		if(rs.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("true"));
		}else{
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("false"));
		}
	}
	/**
	 * 删除一批资源
	 * @param list
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/delResources.do",produces = "application/json")
	@ResponseBody
    public Model delResources(@RequestParam("list") List<Integer> list ,Model model){
		return model;
	}
	
	/**
	 * 分页查询资源
	 * @param list
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/selResources.do",produces = "application/json")
	@ResponseBody
    public void pageResources(HttpServletRequest request,HttpServletResponse response){
		AJAXUtil.setCharacterEncoding(request,response);
		int pageSize = Integer.valueOf(WebUtils.findParameterValue(request, "pageSize"));
		int pageNo = Integer.valueOf(WebUtils.findParameterValue(request, "pageNo"));
		String resCode = WebUtils.findParameterValue(request, "resCode");
		pageNo = (pageNo==0?0:(pageNo*pageSize+1));
		Page page = new Page(pageNo,pageSize);
		Resources resource =new Resources();
		if(resCode != null && !resCode.equals("")){
			resource.setResourceCode(resCode);
		}
		Result result = resourcesService.queryResourceByPage(page,resource);
		Page pageResult = new Page();
		String sb = "";
		if(result.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			pageResult = (Page)result.getResultValue();
			List<Resources> res = new ArrayList<Resources>();
			if(pageResult.getData()!=null){
				res = pageResult.getData();
			}
			sb = ControllerHelper.getResourceDataJsonByList(res);
		}
		String json = "{success:true,pageNo:" + 0 + ",totalPageCount:"
		+ pageResult.getPageCount() + ",totalcount:"+pageResult.getTotalCount()+", data:[" + sb + "]}";
		System.out.println("json##111#"+json);
		AJAXUtil.writerJson(response,json);
	}
	
	/**
	 * 修改资源信息
	 */
	@RequestMapping(value = "/updateResource.do",method=RequestMethod.POST)
	@ResponseBody
	public void updateResource(Resources resources,HttpServletRequest request,HttpServletResponse response){
		Result rs = resourcesService.updateResource(resources);
		if(rs.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("true"));
		}else{
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("false"));
		}
	}
	
	/**
	 * 初始修改用户信息
	 * @param list
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/initUpdateResourceInfo.do",produces = "application/json")
	@ResponseBody
	public void initUpdateResourceInfo(Resources resources,HttpServletRequest request,HttpServletResponse response){
		AJAXUtil.setCharacterEncoding(request,response);
		Result result = resourcesService.queryResourceByResourceCode(resources);
		String infoStr = "";
		String json = "";
		if(result.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			List<Resources> rs = new ArrayList<Resources>();
			resources = (Resources)result.getResultValue();
			if(resources!=null){
				rs.add(resources);
				infoStr = ControllerHelper.getResourcesResDataJsonByList(resources);
				json = "{success:true,userGroupInfo:["+infoStr+"]}";
			}else{
				json = "{success:false,msg:\"查询资源基础信息异常,请联系管理员!\" }";
			}
			
		}else{
			json = "{success:false,msg:\"查询资源基础信息异常,请联系管理员!\" }";
		}
		System.out.println("json123"+json);
		AJAXUtil.writerJson(response,json);
	}
}
